package br.com.mural.android.webservice;

import java.util.ArrayList;
import java.util.List;

import br.com.mural.android.entidades.AlertaAusencia;
import br.com.mural.android.entidades.AlertaEmprego;
import br.com.mural.android.entidades.AlertaProva;
import br.com.mural.android.entidades.Aviso;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

public class AvisoRest {
	private String URL_WS = WebServiceMural.URL_WS + "aviso/";

	public Aviso getAviso(int id) throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + id);

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			Aviso aviso = gson.fromJson(resposta[1], Aviso.class);
			return aviso;
		} else {
			throw new Exception(resposta[1]);
		}
	}

	public List<Aviso> getListaAviso() throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allGSON");

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<Aviso> listaAviso = new ArrayList<Aviso>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				listaAviso.add(gson.fromJson(array.get(i), Aviso.class));
			}
			return listaAviso;
		} else {
			throw new Exception(resposta[1]);
		}
	}
	
	public List<AlertaProva> getListaAlertaProva() throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allAlertaProva");

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<AlertaProva> lista = new ArrayList<AlertaProva>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				lista.add(gson.fromJson(array.get(i), AlertaProva.class));
			}
			return lista;
		} else {
			throw new Exception(resposta[1]);
		}
	}
	
	public List<AlertaProva> getListaAlertaProvaByUsuario(int usuario_id) throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allAlertaProvaByUsuario/" + usuario_id);

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<AlertaProva> lista = new ArrayList<AlertaProva>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				lista.add(gson.fromJson(array.get(i), AlertaProva.class));
			}
			return lista;
		} else {
			throw new Exception(resposta[1]);
		}
	}
	
	public List<AlertaAusencia> getListaAlertaAusencia() throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allAlertaAusencia");

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<AlertaAusencia> lista = new ArrayList<AlertaAusencia>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				lista.add(gson.fromJson(array.get(i), AlertaAusencia.class));
			}
			return lista;
		} else {
			throw new Exception(resposta[1]);
		}
	}
	
	
	public List<AlertaAusencia> getListaAlertaAusenciaByUsuario(int usuario_id) throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allAlertaAusenciaByUsuario/" + usuario_id);

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<AlertaAusencia> lista = new ArrayList<AlertaAusencia>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				lista.add(gson.fromJson(array.get(i), AlertaAusencia.class));
			}
			return lista;
		} else {
			throw new Exception(resposta[1]);
		}
	}
	
	public List<AlertaEmprego> getListaAlertaEmprego() throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allAlertaEmprego");

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<AlertaEmprego> lista = new ArrayList<AlertaEmprego>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				lista.add(gson.fromJson(array.get(i), AlertaEmprego.class));
			}
			return lista;
		} else {
			throw new Exception(resposta[1]);
		}
	}
	
	public List<AlertaEmprego> getListaAlertaEmpregoByUsuario(int usuario_id) throws Exception {

		String[] resposta = new WebServiceMural().get(URL_WS + "allAlertaEmpregoByUsuario/" + usuario_id);

		if (resposta[0].equals("200")) {
			Gson gson = new Gson();
			ArrayList<AlertaEmprego> lista = new ArrayList<AlertaEmprego>();
			JsonParser parser = new JsonParser();
			JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

			for (int i = 0; i < array.size(); i++) {
				lista.add(gson.fromJson(array.get(i), AlertaEmprego.class));
			}
			return lista;
		} else {
			throw new Exception(resposta[1]);
		}
	}

	public String inserirAviso(Aviso aviso) throws Exception {

		Gson gson = new Gson();
		String avisoJSON = gson.toJson(aviso);
		System.out.println("Aviso JSON: " + avisoJSON);
		String[] resposta = new WebServiceMural().post(URL_WS + "inserir", avisoJSON);

		if (resposta[0].equals("200")) {
			return resposta[1];
		} else {
			throw new Exception(resposta[1]);
		}
	}

	public String deletarAviso(int id) {
		String[] resposta = new WebServiceMural().get(URL_WS + "delete/" + id);
		return resposta[1];
	}

}
